package com.neo.base.akkaActorTest

/**
  * Created by hadoop on 6/16/17.
  */
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global


object FutureTest {


  def sleep(millis: Long) = {
    Thread.sleep(millis)
  }

  def doWork(index: Int) = {
    sleep((math.random * 1000).toLong)
    index
  }

  def main(args: Array[String]): Unit = {
    (1 to 5) foreach { index =>
      val future = Future {
        println(index)
        doWork(index)
    }
      future onSuccess {
        case answer: Int => println(s"Success! returned: $answer")
      }
      future onFailure {
        case th: Throwable => println(s"FAILURE! returned: $th")
      }
    }
    sleep(1000) // 等待足够长的时间，以确保工作线程结束。
    println("Finito!")

  }
}
